﻿/**
* 提供对LoadBox的控制
*
* @Experience
 * 当同时有多个LoadBox时,通过LoadManager对所有的LoadBox进行控制先后加载(顺序加载).
 * 
 * 1、当uid已经存在时,
*/

package org.nickro.project.utils{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import org.nickro.project.load.LoadBox;
	import org.nickro.project.events.LoadEvent;
	import org.nickro.project.debug.Console;
	public class LoadManager extends EventDispatcher
	{
		private static var _instance:LoadManager
		
		private var loaderDict:Dictionary = new Dictionary(true);
		private var loaders:Array = [];
		private var loadingIndex:int = 0;
		private var isLoading:Boolean = false;
		//加载断点
		private var breakPoint:int = 0;
		public function LoadManager(singleton:SingletonEnforcer)
		{
		}
		
		public static function getInstance():LoadManager
		{
			if(LoadManager._instance == null)
			{
				_instance = new LoadManager(new SingletonEnforcer());
			}
			return LoadManager._instance;
		}
		
		public function addLoadBox(loader:LoadBox,uid:String):Boolean
		{
			var obj:Object = { };
			//uid === loader.url
			obj.uid = uid;
			obj.target = loader;
			//obj.loadIndex = loadingIndex;
			loaders.push(obj);
			loaderDict[uid] = obj;
			loopLoad();
			return true;
		}

		private function loopLoad():void
		{
			loaders[loadingIndex].target.start();
			loaders[loadingIndex].target.addEventListener(LoadEvent.COMPLETE,onLineComplete);			
		}
		
		public function insertLoad(uid:String):void
		{
			if (loaders[loadingIndex]) {
				if (loaderDict[uid]) {
					Console.info("[LoadManager] :: insertLoad URL == " + uid);
					loaders[loadingIndex].target.stop();
					loaderDict[uid].target.start();
					loaderDict[uid].target.addEventListener(LoadEvent.COMPLETE, onPointComplete);
				}else {
					throw new Error(uid + " is not exist!plz check it out.");
				}
			}else {
				//all Content has been loaded!,so not need to insertLoad
				Console.info("[LoadManager] :: All content has loaded,so no stuff to insertLoad");
			}
		}
		
		private function onLineComplete(e:LoadEvent):void
		{
			e.currentTarget.removeEventListener(LoadEvent.COMPLETE,onLineComplete);	
			//发送LoadEvent.COMPLETE事件
			dispatchEvent(new LoadEvent(LoadEvent.COMPLETE,e.data));		
			if(loadingIndex + 1 == loaders.length)
			{
				restore();
			}else
			{
				loadingIndex++;
				loopLoad();
			}
		}
		
		private function onPointComplete(e:LoadEvent):void
		{
			e.currentTarget.removeEventListener(LoadEvent.COMPLETE, onPointComplete);
			e.currentTarget.dispachEvent(new LoadEvent(LoadEvent.COMPLETE, e.data));
			if (loadingIndex + 1 == loaders.length) {
				restore();
			}else {
				loopLoad();
			}
		}
		/**
		 * 当全部加载完成后,还原LoadManager初始化状态
		 * 
		 */
		public function restore():void
		{
			destroy();
			loadingIndex = 0;
		}
		
		private function destroy():void
		{
			var l:int = loaders.length
			for(var i:int = 0;i < l ; i++)
			{
				loaders.pop();
			}
		}
	}
}
class SingletonEnforcer { };